package com.yjt.fresh.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyMyAddress;
import com.yjt.fresh.api.ApiDeleteAddress;
import com.yjt.fresh.api.ApiGetAddressList;
import com.yjt.fresh.api.ApiSetDefaultAddress;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanAddress;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.CustomToastResult;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的地址
 * Created by mcc on 2018/8/6.
 */

public class AtyMyAddress extends BaseSimpleActivity {
    private static final int REQUEST_CODE = 1006;
    @BindView(R.id.rlBack)
    RelativeLayout mRlBack;
    @BindView(R.id.rvAppAtyMyAddress)
    RecyclerView mRv;
    @BindView(R.id.tvAppAtyMyAddressAppendNewAddress)
    TextView mTvAppendNewAddress;
    private AdpRvAtyMyAddress mAdpRvAtyMyAddress;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_my_address;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        initRV();
        initClick();
        getData();
    }


    private List<BeanAddress> mBeanAddressList;
    HttpOnNextListener<List<BeanAddress>> mAddressListener = new HttpOnNextListener<List<BeanAddress>>() {
        @Override
        public void onNext(List<BeanAddress> beanAddresses) {
            mBeanAddressList = beanAddresses;
            mAdpRvAtyMyAddress.setNewData(mBeanAddressList);
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };
    private void getData() {
        ApiGetAddressList apiGetAddressList = new ApiGetAddressList(mAddressListener,mContext,true,false,false);
        Map<String,String> map = new HashMap<>();
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiGetAddressList.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetAddressList);
    }

    private void initClick() {
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTvAppendNewAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,AtyAppendAddress.class);
                intent.putExtra(MyConstants.ADD_OR_UPDATE_ADDRESS,MyConstants.ADD);
                startActivityForResult(intent,REQUEST_CODE);
            }
        });

        mAdpRvAtyMyAddress.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                if (view.getId() == R.id.rbAppRecycleItemAtyMyAddress){
                    setDefaultAddress(position);
                }
                if (view.getId() == R.id.tvAppRecycleItemAtyMyAddressAlter){
                    Intent intent = new Intent(mContext,AtyAppendAddress.class);
                    intent.putExtra(MyConstants.ADD_OR_UPDATE_ADDRESS,MyConstants.UPDATE);
                    intent.putExtra(MyConstants.ADDRESS_OBJ,mAdpRvAtyMyAddress.getData().get(position));
                    startActivityForResult(intent,REQUEST_CODE);
                }

                if (view.getId() == R.id.tvAppRecycleItemAtyMyAddressDelete){
                    sureOrCancelDialog(position);
                }
            }
        });
    }

    HttpOnNextListener<BaseResultEntity> mDeleteAddressListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            CustomToastResult.showLong("删除成功");
            getData();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };
    private void deleteAddress(int position) {
        ApiDeleteAddress apiDeleteAddress = new ApiDeleteAddress(mDeleteAddressListener,mContext,true,false,false);
        Map<String,Object> map = new HashMap<>();
        map.put("SESSION_ID",PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        map.put("SHIPPINGADDRESS_ID",mAdpRvAtyMyAddress.getData().get(position).getSHIPPINGADDRESS_ID());
        apiDeleteAddress.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiDeleteAddress);
    }

    HttpOnNextListener<BaseResultEntity> mDefaultAddressListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            CustomToastResult.showLong("设置成功");
            getData();
        }

        @Override
        public void onReLogin() {

        }
    };
    private void setDefaultAddress(int position) {
        if ("0".equals(mAdpRvAtyMyAddress.getItem(position).getIS_DEFAULT())) {
            ApiSetDefaultAddress apiSetDefaultAddress = new ApiSetDefaultAddress(mDefaultAddressListener,mContext,true,false,true);
            Map<String,Object> map = new HashMap<>();
            map.put("SESSION_ID",PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
            map.put("SHIPPINGADDRESS_ID",mAdpRvAtyMyAddress.getData().get(position).getSHIPPINGADDRESS_ID());
            apiSetDefaultAddress.setMap(map);
            HttpManager.getInstance().doHttpDeal(apiSetDefaultAddress);
        }
    }

    private void initRV() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        List<BeanAddress> addressList = new ArrayList<>();
        mAdpRvAtyMyAddress = new AdpRvAtyMyAddress(R.layout.app_recycle_item_my_address, addressList);
        mRv.setAdapter(mAdpRvAtyMyAddress);
        mAdpRvAtyMyAddress.bindToRecyclerView(mRv);
        mAdpRvAtyMyAddress.setEmptyView(R.layout.view_empty);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE){
            getData();
        }
    }

    private void sureOrCancelDialog(final int position) {
        final Dialog dialog = new Dialog(mContext, R.style.MyDialog);
        dialog.setContentView(R.layout.app_dialog_sure_cancel);
        TextView tvContent = dialog.findViewById(R.id.tvAppDIalogSureCancelContent);
        TextView tvCancel = dialog.findViewById(R.id.tvAppDialogSureCancelCancel);
        TextView tvSure = dialog.findViewById(R.id.tvAppDialogSureCancelSure);
        tvContent.setText("确认删除该收货地址吗?");
        tvCancel.setText("暂不删除");
        tvSure.setText("确认删除");
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteAddress(position);
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialog.show();
    }
}
